/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * composePMPanel.java
 *
 * Created on 24/Out/2012, 11:02:31
 */
package chatclient;

import com.sd.chat.Message;
import com.sd.chat.Utilizador;
import com.sd.chat.WrapLayout;
import java.awt.FlowLayout;
import java.util.Vector;
import javax.swing.JLabel;
import org.jdesktop.application.Action;

/**
 *
 * @author Old
 */
public class composePMPanel extends javax.swing.JPanel {

    private ChatSDClientView pai;
    private Vector<Utilizador> listaDeUsers;
    private Vector<Integer> toIds;
    private Utilizador from;
    private int tipo = 0;

    /** Creates new form composePMPanel */
    public composePMPanel(Message mess, Vector<Utilizador> listaDeUsers) {
        initComponents();
        this.listaDeUsers = listaDeUsers;
        jPanel1.setLayout(new WrapLayout(FlowLayout.LEFT, 2, 2));
        this.from = mess.getFrom();
        toIds = mess.getTo();

        jTextArea1.setText(mess.getText());
        jTextArea1.setCaretPosition(0);
        for (int j = 0; j < toIds.size(); j++) {
            boolean found = false;
            for (int i = 0; i < this.listaDeUsers.size(); i++) {
                if (this.listaDeUsers.get(i).getId() == toIds.get(j)) {
                    found = true;
                    jPanel1.add(new JLabel(this.listaDeUsers.get(i).getNome() + ";"));
                }
            }
            if (!found) {
                jPanel1.add(new JLabel("userid:" + toIds.get(j) + ";"));
            }
        }

        deUsername.setText(this.from.getNome());

        listaUsers.setVisible(false);
        butAddTo.setVisible(false);
        jTextArea1.setEditable(false);
        butEnviar.setVisible(false);
    }

    public composePMPanel(ChatSDClientView pai, Vector<Utilizador> listaDeUsers, Utilizador from) {
        initComponents();
        tipo = 1; //nova mensagem
        jPanel1.setLayout(new WrapLayout(FlowLayout.LEFT, 2, 2));
        this.pai = pai;
        this.listaDeUsers = listaDeUsers;
        this.from = from;
        this.toIds = new Vector<Integer>();

        deUsername.setText(this.from.getNome());

        setAddList();
    }

    public composePMPanel(ChatSDClientView pai, Vector<Utilizador> listaDeUsers, Utilizador from, Message mess) {
        initComponents();
        tipo = 2; //responder mensagem
        jPanel1.setLayout(new WrapLayout(FlowLayout.LEFT, 2, 2));
        this.pai = pai;
        this.listaDeUsers = listaDeUsers;
        this.from = from;
        this.toIds = new Vector<Integer>();

        jTextArea1.setText("\n\n-----------\nDE: " + mess.getFrom().getNome() + "\nPARA: " + from.getNome() + "\nDATA: " + mess.getData().getTime() + "\n" + mess.getText() + "\n-----------\n");
        jTextArea1.setCaretPosition(0);

        deUsername.setText(this.from.getNome());

        boolean found = false;
        for (int i = 0; i < this.listaDeUsers.size(); i++) {
            if (this.listaDeUsers.get(i).getId() == mess.getFrom().getId()) {
                found = true;
                //System.out.println("composePMPanel from: "+mess);
                toIds.add(mess.getFrom().getId());
                jPanel1.add(new JLabel(mess.getFrom().getNome() + ";"));
                break;
            }
        }
        if (!found) {
            toIds.add(mess.getFrom().getId());
            this.listaDeUsers.add(mess.getFrom());
            jPanel1.add(new JLabel(mess.getFrom().getNome() + ";"));
        }
        setAddList();
    }

    private void setAddList() {
        listaUsers.removeAll();
        String[] nomesUsers = new String[this.listaDeUsers.size()];
        for (int i = 0; i < this.listaDeUsers.size(); i++) {
            nomesUsers[i] = this.listaDeUsers.get(i).getNome();
        }
        listaUsers.setListData(nomesUsers);
        listaUsers.revalidate();
        listaUsers.repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        deUsername = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        listaUsers = new javax.swing.JList();
        butAddTo = new javax.swing.JButton();
        pmText = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        butEnviar = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(800, 178));
        setMinimumSize(new java.awt.Dimension(800, 178));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(800, 178));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(chatclient.ChatSDClientApp.class).getContext().getResourceMap(composePMPanel.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 595, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(jPanel1);

        deUsername.setText(resourceMap.getString("deUsername.text")); // NOI18N
        deUsername.setName("deUsername"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N
        jScrollPane2.setPreferredSize(new java.awt.Dimension(23, 130));

        listaUsers.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        listaUsers.setName("listaUsers"); // NOI18N
        jScrollPane2.setViewportView(listaUsers);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(chatclient.ChatSDClientApp.class).getContext().getActionMap(composePMPanel.class, this);
        butAddTo.setAction(actionMap.get("clickedAddTo")); // NOI18N
        butAddTo.setText(resourceMap.getString("butAddTo.text")); // NOI18N
        butAddTo.setName("butAddTo"); // NOI18N

        pmText.setName("pmText"); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setName("jTextArea1"); // NOI18N
        pmText.setViewportView(jTextArea1);

        butEnviar.setAction(actionMap.get("clickedSendNewMessage")); // NOI18N
        butEnviar.setText(resourceMap.getString("butEnviar.text")); // NOI18N
        butEnviar.setName("butEnviar"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(deUsername)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(pmText)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 614, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(butAddTo)
                            .addComponent(butEnviar)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(deUsername))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel1)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(butAddTo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(butEnviar))
                    .addComponent(pmText, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void clickedAddTo() {
        int[] selectedIdxs = listaUsers.getSelectedIndices();
        for (int i = 0; i < selectedIdxs.length; i++) {
            Utilizador userEscolhido = this.listaDeUsers.get(selectedIdxs[i]);
            if (!toIds.contains(userEscolhido.getId())) {
                toIds.add(userEscolhido.getId());
                jPanel1.add(new JLabel(userEscolhido.getNome() + ";"));
            }
        }
        jPanel1.revalidate();
        jPanel1.repaint();

    }

    @Action
    public void clickedSendNewMessage() {
        if (toIds.size() > 0) {
            Message tempMessage = new Message(this.from, toIds, jTextArea1.getText());
            this.pai.newMessageSubmit(tempMessage);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton butAddTo;
    private javax.swing.JButton butEnviar;
    private javax.swing.JLabel deUsername;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JList listaUsers;
    private javax.swing.JScrollPane pmText;
    // End of variables declaration//GEN-END:variables
}
